﻿/**
  KCAKCAK   CAKCAKC   AK      CA
 AK        KC     CA  AK    AK
CA         KC     CA  AK  KC
CA         KCAKCAKCA  AKCA
CA         KC     CA  AK  KC
 AK        KC     CA  AK    AK
  KCAKCAK  KC     CA  AK      CA

_________________________________________________________________
 China ataca Kamchatka - copyright 2007(c) Ruben Sainz de la Maza
 
	@class: 	Countries
	@vertion: 	0.1
	@date:		22/11/07
				
	@language: 	ActionScript 3.0
	@author:	Ruben Sainz de la Maza
	@usage: 
		_objCountries = Countries.getInstance();
			
**/

package com.conelmate.cak.board.countries
{
	
	import com.conelmate.util.Trace;
	import com.conelmate.cak.board.data.CAKdata;
	import com.conelmate.cak.events.CountryEvent;
	
	import flash.display.Sprite;
	import flash.display.DisplayObject;
	
	
	public class Countries extends Trace implements ICountry
	{
		private	static	var	_INSTANCE			:Countries;			//instancia de esta clase (singleton)
		private static	var _PARENT				:DisplayObject;		//ref al root
		private 		var	_isFreeze			:Boolean = false;	//true:congela eventos, false:reactiva eventos
		private			var _isInited			:Boolean = false;	//false:la clase aun no ha sido iniciada
		
		private 		var _objCAKdata			:CAKdata;			//datos centralizados
		private 		var _objData			:Object;			//datos de paises obtenidos desde CAKdata
		private 		var _objCountries		:Object;			//paises generados a partir de los datos
				

		
//_________________________________________________________________________________ Constructor
		public function Countries(enforcer:SingletonEnforcer)
		{
			_isTrace = false;
			mTrace("#Countries# constructor");
			
		//	init();
		}		
		

//_________________________________________________________________________________ Public Methods

		/**
		 * Singleton.  Devuelve una instancia unica de esta clase
		 */
		public static function getInstance():Countries
		{
			if(Countries._INSTANCE == null) Countries._INSTANCE = new Countries(new SingletonEnforcer());
			return Countries._INSTANCE;
		}
		
		
		/**
		 * Inicializa la clase
		 */
		public function init(parent:DisplayObject = null):void
		{
			if(!_isInited){
				mTrace("#Countries# init");
				_isInited = true;
				_PARENT = parent || _PARENT;
				
				//codigo de inicializacion
				if(!_objCountries) setCountries();
			}			
		}
		

		/**
		 * Destruye las instancias
		 */
		public function destroy():void
		{
			mTrace("#Countries# destroy");
			_objCountries = null;
			_isInited = false;
			Countries._INSTANCE = null;
		}
		
		
		/**
		 * Congela / descongela los eventos de la clase
		 * @param   freeze 
		 */		
		public function freeze(freeze:Boolean):void
		{
			mTrace("#Countries# freeze");
			_isFreeze = freeze;
		}
		

//_________________________________________________________________________________ Private Methods

		/**
		 * obtener los datos de los paises desde CAKdata y guardarlos localmente en _objCountries
		 */		
		private function setCountries():void
		{
			mTrace("#Countries# setCountries");
			
			_objCAKdata = CAKdata.getInstance();
			_objData = _objCAKdata.objCountries;

			//verificar:
		//	for(var i in _objData) trace(i+" "+_objData[i].continent.name);
		//	for(var i in _objData) trace(_objData[i].player+" "+i);
		
			constructCountries();
		}


		/**
		 * crear los paises a partir de los datos obtenidos de CAKdata
		 */		
		private function constructCountries():void
		{
			mTrace("#Countries# constructCountries");

			_objCountries = new Object();
			for(var i in _objData){
				_objCountries[_objData[i].index] = new Country(_objData[i]);
				_objCountries[_objData[i].index].addEventListener(CountryEvent.EVENT, onCountryEvent);
				_objCountries[_objData[i].index].init(_PARENT);
			}
		}
		

		private function onCountryEvent(e:CountryEvent):void 
		{
			this[e.func](e.param);			
		}
		
		
		private function limitrofes(param:Object):void
		{
			var arrLimitrofes:Array = _objData[param.country].limits;
		//	trace("arrLimitrofes:"+arrLimitrofes);

			for(var i in arrLimitrofes){
				var index = _objCAKdata.getCountryIndex(arrLimitrofes[i]).index;
				_objCountries[index].freeze(param.visible);
			}
		}

//_________________________________________________________________________________ GETTERS


		
//_________________________________________________________________________________ SETTERS

	//	public function _container	(p)	{ _CONTAINER = p; trace("_CONTAINER:" +_CONTAINER) }
		
	}
}

class SingletonEnforcer{}